Java syntax
part 5/36 Β· 136.0 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β’ false
β’ null
Unused
The following words are reserved as keywords, but currently have no use
or purpose.
β’ const
β’ goto
β’ strictfp
Literals
| Integers | |
|---|---|
| binary (introduced in Java SE 7) | 0b11110101 ( 0b followed by a binary nu⦠|
| octal | 0365 ( 0 followed by an octal number) |
| hexadecimal | 0xF5 ( 0x followed by a hexadecimal num⦠|
| decimal | 245 (decimal number) |
| Floating-point values | |
| float | 23.5F , .5f , 1.72E3F (decimal fraction⦠|
| float | 0x.5FP0F , 0x.5P-6f ( 0x followed by a⦠|
| double | 23.5D , .5 , 5. , 1.72E3D (decimal frac⦠|
| double | 0x.5FP0 , 0x.5P-6D ( 0x followed by a h⦠|
| Character literals | |
| char | 'a' , 'Z' , '\u0231' (character or a c⦠|
| Boolean literals | |
| boolean | true , false |
| null literal | |
| null reference | null |
| String literals | |
| String | "Hello, World" (sequence of characters⦠|
| Characters escapes in strings | |
| Unicode character | \u3876 ( \u followed by the hexadecim⦠|
| Octal escape | \352 (octal number not exceeding 377,β¦ |
| Line feed | \n |
| Carriage return | \r |
| Form feed | \f |
| Backslash | \\ |
| Single quote | \' |
| Double quote | \" |
| Tab | \t |
| Backspace | \b |
Integer literals are of int type by default unless long type is
specified by appending L or l suffix to the literal, e.g. 367L. Since
Java SE 7, it is possible to include underscores between the digits of a
number to increase readability; for example, a number 145608987 can be
written as 145_608_987.
Variables
Variables are identifiers associated with values. They are declared by
writing the variable's type and name, and are optionally initialized in
the same statement by assigning a value.
int count; //Declaring an uninitialized variable called 'count', of type
'int'
count = 35; //Initializing the variable
int count = 35; //Declaring and initializing the variable at the same
time
Multiple variables of the same type can be declared and initialized in
one statement using comma as a delimiter.
int a, b; //Declaring multiple variables of the same type
int a = 2, b = 3; //Declaring and initializing multiple variables of the
same type
Type inference
Since Java 10, it has become possible to infer types for the variables
automatically by using var.
// stream will have the FileOutputStream type as inferred from its
initializer
var stream = new FileOutputStream("file.txt");
// An equivalent declaration with an explicit type
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ